package cn.edu.scau.cmi.ema.util;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.mapping.Collection;

import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


public class CmiEntityUtil {
	public static void getAllFields(Class entityClass) {
		Field[] fields = entityClass.getDeclaredFields();
		for (Field field : fields) {
			Class<?> fieldType = field.getType();
			String fieldName = field.getName();
			System.out.println(fieldName + "的类型是：" + field.getType());
		}

	}

	public static Set<String> getAllFieldNames(Class<?> entityClass) {
		Set<String> allFiledNames = new HashSet<String>();
		Field[] fields = entityClass.getDeclaredFields();
		for (Field field : fields) {
			String fieldName = field.getName();
			allFiledNames.add(fieldName);
		}
		return allFiledNames;
	}

	public static Set<String> getAllBasicFieldNames(Class<?> entityClass) {
		Set<String> allFiledNames = new HashSet<String>();
		Field[] fields = entityClass.getDeclaredFields();
		for (Field field : fields) {
			String fieldName = field.getName();
			Class<?> fieldType = field.getType();
			if (fieldType.equals(String.class) || fieldType.equals(Integer.class) || fieldType.equals(Calendar.class) || fieldType.equals(Boolean.class)) {
				allFiledNames.add(fieldName);
			}
		}
		return allFiledNames;
	}

	public static Map<String, Class<?>> getAllFieldNameTypeMap(Class<?> entityClass) {
		Map<String, Class<?>> allFieldNameTypeMap = new HashMap<String, Class<?>>();
		Field[] fields = entityClass.getDeclaredFields();
		for (Field field : fields) {
			Class<?> fieldType = field.getType();
			String fieldName = field.getName();
			allFieldNameTypeMap.put(fieldName, fieldType);
		}
		return allFieldNameTypeMap;
	}
	
	
	
	

	public static Map<String, Class<?>> getAllBasicFieldNameTypeMap(Class<?> entityClass) {
		Map<String, Class<?>> allFieldNameTypeMap = new HashMap<String, Class<?>>();
		Field[] fields = entityClass.getDeclaredFields();
		for (Field field : fields) {
			Class<?> fieldType = field.getType();
			String fieldName = field.getName();
			if (fieldType.equals(String.class) || fieldType.equals(Integer.class) || fieldType.equals(Calendar.class)) {
				allFieldNameTypeMap.put(fieldName, fieldType);
			}

		}
		return allFieldNameTypeMap;
	}

//	返回集合类型的域名与分页对象，获取域名events4parents与集合类型中的元素的类型Set<Event>合适
//	events4parents -> Event
	public static <T> Map<String, Class<?>> getCollectionFieldNameTypeMap(Class<T> entityClass) {
		Map<String, Class<?>> collectionFieldNameTypeMap = new HashMap();
		Field[] fields = entityClass.getDeclaredFields();
		for (Field field : fields) {
			String fieldName = field.getName();
			
			Class<?> fieldClass = field.getType();//java.util.Set()，不包含集合中具体的元素的类型
			
			Type fieldType = field.getGenericType();//java.util.Set<cn.edu.scau.cmi.ema.domain.Food>
			
//			添加集合类型，所有的Collection类型应该都可以。
//			if(Collection.class.isAssignableFrom(fieldType)) {
			if(fieldClass == Set.class) {
//				把集合中的数据解析处理
				
//				获取集合类中的元素的类型名称
				String typeName = fieldType.getTypeName();//
				String f = typeName.substring(typeName.indexOf('<') + 1, typeName.indexOf('>'));
				try {
					Class<?> refClass =  Class.forName(f);
					collectionFieldNameTypeMap.put(fieldName, refClass);
//					System.out.println("---域名是："  + fieldName + "，类型是：" + refClass.getName());
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				
//				System.out.println(typeName);
//				System.out.println("!!!域名称是：" + fieldName + "，类型是：" + fieldClass.getName());
//				collectionFieldNameTypeMap.put(fieldName + "Pagination", fieldClass);
			}
		}
		return collectionFieldNameTypeMap;
	}

}
